//app.js
App({
	onLaunch: function () {
		//初始化购物车
		//this.scanCart(this);
  
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.request({
          url: this.globalData.url + 'token/user',
          method: 'POST',
          data: {
            code: res.code
          },
          header: {
            'content-type': 'application/json;charset=utf-8'
          },
          success: res => {
            console.log('token', res.data.token)
            this.globalData.token = res.data.token;
            wx.setStorageSync('token', res.data.token);
            if (this.checkLoginReadyCallback) {
              this.checkLoginReadyCallback(res);
            }
            //初始化购物车
            this.scanCart(this);
          }
        });
      }
    })

		
		// this.scanCart()


	},
	globalData: {
		userInfo: null,
		token: '',
		url: 'https://www.lythds.com/'
		// url: 'http://acbkshoptest.com/'
	},
	//首先定义了一个方法
	timer: false,
	scanCart: function (that) {
		console.log('22222', this.globalData.url);
		let cartNumber = 0;

		console.log('获取token', wx.getStorageSync('token'))
		// 发送 res.code 到后台换取 openId, sessionKey, unionId
		wx.request({
			url: this.globalData.url + 'cartCount ',
			method: 'POST',
			data: {

			},
			header: {
				'content-type': 'application/json;charset=utf-8',
        token: wx.getStorageSync('token') || this.globalData.token
			},
			success: res => {
				console.log('购物车总数', res.data.data)
				cartNumber = res.data.data
				if (cartNumber) { //判断购物车的数量个数，购物车如果为空就走else
          wx.setStorageSync("cartNumber", cartNumber)
					wx.setTabBarBadge({ //购物车不为空 ，给购物车的tabar右上角添加购物车数量标志
						index: 3, //标志添加位置
						text: "" + cartNumber + "" //通过编译，将购物车总数量放到这里
					})
          
				} else { //购物车为空
					wx.removeTabBarBadge({ //移除指定位置的tabbar右上角的标志
						index: 3,
					})
				}

			}
		});

		//购物车

		console.log('取缓存中的总数据-------------', wx.getStorageSync("cartNumber"))

	},
  onShow(){
    console.log('app show')
  },
})
